#include "tcp2485.h"
#include "tcp.h"
#include "util.h"
#include <vector>



bool getReadParam(string ip,MachineReadParam &param)
{
      //功能码0x03(读多路寄存器输入),寄存器起始地址0x0001,(占用40002,40003,40004,40005),读4个字的数据
    // 01 03 00 01 00 04 CRC CRC
    unsigned char arr[8] = {0x01, 0x03, 0x00, 0x01, 0x00, 0x04};
    uint16_t crc =CRC_Check(arr,6);
    arr[6] = crc >> 8;
    arr[7] = crc << 8 >>8;

    string  recv;
    wirteToServer(ip, arr, 8,recv);
    return true;
}


bool MachineWirteParam::exec()
{
     //目前版本只支持写多路寄存器，所以需要需要所有设置项
    //功能码0x10(写多路寄存器),寄存器起始地址0x0005,(占用40006,40007,40008),写3个字的数据
    //01 10 00 05 00 03 06

    unsigned char arr[15] ={0x01, 0x10, 0x00,0x05,0x00,0x03,0x06};

    //word0 H
    m_enable = true;//应该一直是true
    m_enable ? arr[7] = (arr[7] | 1) : arr[7] = (arr[7] & ~1);
    m_lightWarn ?arr[7] = (arr[7] | (1<< 1)) : arr[7] =(arr[7] & ~(1 <<1));
    m_fatalWarn ? arr[7] = (arr[7] | (1<< 2)) : arr[7] =(arr[7] & ~(1 <<2));

    //word0 L
    arr[8] = m_direction;

    //word 1
    uint16_t leftSpeed = m_leftSpeed;
    arr[9] = leftSpeed >> 8;
    arr[10] = leftSpeed << 8 >>8;

    //word 2
    uint16_t rightSpeed = m_rightSpeed;
    arr[11] = rightSpeed >> 8;
    arr[12] = rightSpeed << 8 >>8;

    uint16_t crc =CRC_Check(arr,14);
    arr[13] = crc >> 8;
    arr[14] = crc << 8 >>8;

    std::string str;
    for(auto i : arr)
{
        str += unsignedCharToHex(i) + " ";
}

    printf("writMulti req:%s",str.c_str());

    string  recv;
    wirteToServer(m_ip, arr, sizeof(arr),recv);
    return true;

    /*
    QByteArray qArr((char *)arr, sizeof(arr)/sizeof(unsigned char));

    qDebug() << " writMulti req:" << str.c_str();

    QByteArray rsp = syncSendData(qArr);
    const char *rspChar = rsp.constData();
    int size = rsp.size();
    if(size < 2 || !rspChar)
    {
        return false;
    }

    str.clear();
    for(auto i : rsp)
    {
        str += unsignedCharToHex(i) + " ";
    }
    qDebug() << " writMulti rsp:" << str.c_str();

    if(rspChar[1] != 0x10)
{
        qWarning() << " writMulti rsp err:" << unsignedCharToHex(rspChar[1]).c_str();
    }

    return true;
    */
}

void MachineWirteParam::setFatalWarn(bool b)
{
    m_fatalWarn = b;
}

void MachineWirteParam::setlightWarn(bool b)
{
    m_lightWarn = b;
}

void MachineWirteParam::setDirection(Direction d)
{
    m_direction =d;
}

void MachineWirteParam::setLeftSpeed(unsigned int speed)
{
    if( speed > 3000)
    {
        speed = 3000;
    }

    m_leftSpeed = speed;
}

void MachineWirteParam::setRightSpeed(unsigned int speed)
{
    if( speed > 3000)
    {
        speed = 3000;
    }

    m_rightSpeed = speed;
}

void MachineWirteParam::setEnable(bool b)
{
    m_enable = b;
}

string MachineWirteParam::getIp()
{
    return m_ip;
}

bool MachineReadParam::getFatalWarn()
{
    return m_fatalWarn;
}

bool MachineReadParam::isLoseNet()
{
    return m_bLoseNet;
}

Direction MachineReadParam::getDirection()
{
    return m_direction;
}

unsigned int MachineReadParam::getLeftSpeed()
{
    return m_leftSpeed;
}

unsigned int MachineReadParam::getRightSpeed()
{
    return m_rightSpeed;
}

bool MachineReadParam::getMashineState()
{
    return m_bStart;
}



